<?php

namespace app\admin\controller\coupon;

use app\admin\traits\Curd;
use app\common\controller\AdminController;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\model\CouponCoupon;
use think\App;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;

/**
 * @ControllerAnnotation(title="优惠券管理")
 */
class Coupon extends AdminController
{
    protected $allowModifyFields = [
        'status',
        'sort',
        'remark',
        'is_delete',
        'is_auth',
        'title',
        'new_limit'
    ];
    protected array $rule = [];
    use Curd;

    protected array $withAttr = [];

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new CouponCoupon();

    }

    /**
     * @NodeAnotation (title="添加")
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $post['start_time'] = strtotime($post['start_time']);
            $post['end_time'] = strtotime($post['end_time']);
            if(!empty($post['use_end_time'])){
                $post['use_end_time'] = strtotime($post['use_end_time']);
            }else{
                $post['use_end_time'] = 0;
            }

            $this->rule = [
                'title|优惠券名称' => 'require|max:64',
                'face_val|面额' => 'require|number',
                'use_limit|使用条件' => 'require|number',
                'use_desc|使用说明' => 'require|max:500',
                'count|发放数量' => 'require|number',
                'start_time|领券开始时间' => 'number',
                'end_time|领券结束时间' => 'number',
                'use_end_time|使用结束时间' => 'number',
                'use_days|使用天数' => "number",
            ];
            $this->validate($post, $this->rule);
            if ($post['use_time_type'] == 1) {
                if (empty($post['start_time'])) {
                    return Show::json_show(Status::status_error, "请选择开始时间");
                }
                if (empty($post['end_time'])) {
                    return Show::json_show(Status::status_error, "请选择结束时间");
                }
                if (empty($post['use_end_time'])) {
                    return Show::json_show(Status::status_error, "请选择使用结束时间");
                }
                if ($post['start_time'] > $post['end_time']) {
                    return Show::json_show(Status::status_error, "结束时间不能大于开始时间");
                }
                if ($post['end_time'] > $post['use_end_time']) {
                    return Show::json_show(Status::status_error, "使用结束时间不能小于结束时间");
                }
            } else {
                if (intval($post['use_days']) <= 0) {
                    return Show::json_show(Status::status_error, "请输入天数");
                }
            }
            if (empty($post['use_type'])) {
                return Show::json_show(Status::status_error, "请选择使用类型");
            }
            $post['surplus_count'] = $post['count'];
            if($post['use_type'] == 2){
                if(empty($post['goods_list'])){
                    return Show::json_show(Status::status_error,"请选择指定商品");
                }
            }
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            //保存商品信息
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $post['start_time'] = strtotime($post['start_time']);
            $post['end_time'] = strtotime($post['end_time']);
            $post['use_end_time'] = strtotime($post['use_end_time']);

            $this->rule = [
                'title|优惠券名称' => 'require|max:64',
                'face_val|面额' => 'require|number',
                'use_limit|使用条件' => 'require|number',
                'use_desc|使用说明' => 'require|max:500',
                'count|发放数量' => 'require|number',
                'start_time|领券开始时间' => 'number',
                'end_time|领券结束时间' => 'number',
                'use_end_time|使用结束时间' => 'number',
                'use_days|使用天数' => "number",
            ];

            $this->validate($post, $this->rule);
            if ($post['use_time_type'] == 1) {
                if (empty($post['start_time'])) {
                    return Show::json_show(Status::status_error, "请选择开始时间");
                }
                if (empty($post['end_time'])) {
                    return Show::json_show(Status::status_error, "请选择结束时间");
                }
                if (empty($post['use_end_time'])) {
                    return Show::json_show(Status::status_error, "请选择使用结束时间");
                }
                if ($post['start_time'] > $post['end_time']) {
                    return Show::json_show(Status::status_error, "结束时间不能大于开始时间");
                }
                if ($post['end_time'] > $post['use_end_time']) {
                    return Show::json_show(Status::status_error, "使用结束时间不能小于结束时间");
                }
            } else {
                if (intval($post['use_days']) <= 0) {
                    return Show::json_show(Status::status_error, "请输入天数");
                }
            }
            if (empty($post['use_type'])) {
                return Show::json_show(Status::status_error, "请选择使用类型");
            }
            $post['surplus_count'] = $post['count'];
            if($post['use_type'] == 2){
                if(empty($post['goods_list'])){
                    return Show::json_show(Status::status_error,"请选择指定商品");
                }
            }
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }
    public function push(){
        $id = $this->request->param('id');
        //推送信息
        try {
            $push = $this->model->updateDataSave($id, [
                'push' => 1,
                'update_time' => time()
            ]);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error,$e->getMessage());
        }
        if($push){
            return Show::json_show(Status::status_normal,"推送成功");
        }else{
            return Show::json_show(Status::status_error, "推送失败");
        }
    }

}